//
//  gameData.m
//  CountDown2
//
//  Created by mmac on 11/15/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "gameData.h"


@implementation GameData

@synthesize data;

- (NSDictionary *)getLevel{

	NSArray *levels = [data valueForKey:@"Levels"];

	NSUInteger count = [levels count];
	NSUInteger index = arc4random() % count;
	NSDictionary *level = [levels objectAtIndex:index];
	
	return level;
}

- (id)init {
	if ((self = [super init])) {
        /* class-specific initialization goes here */
		[self loadData];
    }
    return self;
}

- (BOOL)loadData {
	
	NSString *errorDesc = nil;
	NSPropertyListFormat format;
	NSString *plistPath;
	NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
															  NSUserDomainMask, YES) objectAtIndex:0];
	plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
	if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
		plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
	}
	NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
	NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
										  propertyListFromData:plistXML
										  mutabilityOption:NSPropertyListMutableContainersAndLeaves
										  format:&format
										  errorDescription:&errorDesc];
	if (!temp) {
		NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
	}
	
	data = [temp copy];
	
	return YES;
}

- (void)dealloc {
	NSLog(@"gamedata delloc.");
	[data release];
    [super dealloc];
}

@end
